package com.h3bpm.handler;

import OThinker.H3.Entity.EventHandlers.IInstanceEventHandler;
import OThinker.H3.Entity.IEngine;
import OThinker.H3.Entity.Instance.InstanceContext;
import OThinker.H3.Entity.Instance.InstanceState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** 流程事件
        * Created by wjq on 2019/12/12.
        */
public class InstanceHandler implements IInstanceEventHandler {
    private static final Logger logger = LoggerFactory.getLogger(InstanceHandler.class);

    @Override
    public void OnCreated(IEngine iEngine, InstanceContext instanceContext) {
        logger.info("《》《》《》流程实例接口创建 instanceContext instanceContext={}, State={}",instanceContext.getInstanceId() ,instanceContext.getState());
        boolean b = false;
         UserProtalHelper.createInstance(iEngine, instanceContext);
        if(b){
            logger.info("流程实例创建结果====================成功");
        }else {
            logger.info("流程实例创建结果====================失败");
        }

    }

    @Override
    public void OnUpdated(IEngine iEngine, InstanceContext instanceContext) {
        logger.info("《》《》《》流程实例接口OnUpdated instanceContext instanceContext={}, State={}",instanceContext.getInstanceId() ,instanceContext.getState());

        if(InstanceState.Finished.equals(instanceContext.getState())){
            boolean b = false;
            b = UserProtalHelper.updateInstance(iEngine, instanceContext);
            if(b){
                logger.info("流程实例结束结果====================成功");
            }else {
                logger.info("流程实例结束结果====================失败");
            }
            return;
        }
        if(InstanceState.Canceled.equals(instanceContext.getState())){
            boolean b = false;
            b =  UserProtalHelper.updateInstance(iEngine,instanceContext);
            if(b){
                logger.info("流程实例结束结果====================成功");
            }else {
                logger.info("流程实例结束结果====================失败");
            }
            return;
        }
    }

    @Override
    public void OnRemoved(IEngine iEngine, String s) throws Exception {
        logger.info("《》《》《》流程实例接口OnRemoved instanceContext instanceContext={}, State={}");

    }
}
